package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.article.mapper.ApArticleConfigMapper;
import com.usian.article.mapper.ApArticleContentMapper;
import com.usian.article.service.ArticleInfoService;
import com.usian.common.exception.CatchCustomException;
import com.usian.common.exception.code.UserStatusCode;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings("ALL")
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        if(dto==null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"传输对象不能为空");
        }
        if(dto.getArticleId() == null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"查询不到对应文章信息");
        }

        Map<String, Object> resultMap = new HashMap<>();
        // 文章配置信息 (和文章 1-1 关系)
        ApArticleConfig apArticleConfig =  apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        resultMap.put("config",apArticleConfig);

        if(!apArticleConfig.getIsDown() && !apArticleConfig.getIsDelete()){
            // 文章图文内容 (和文章 1-1 关系)
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            resultMap.put("content",apArticleContent);
        }
        return ResponseResult.okResult(resultMap);
    }
}
